package sopia.conn;

import sopia.cmm.CommonFunct;
import sopia.cmm.Constant;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AccountAdapter {
	 /** Called when the activity is first created. */
	private DBAdapter dbAdapter ;
	private SQLiteDatabase db;  

	public AccountAdapter(Context context){
		  dbAdapter = new DBAdapter(context);
		  db = dbAdapter.getSqlDB();
	}
	
	public void beginTransaction(){
		db.beginTransaction();
	}
	public void setTransactionSuccessful(){
		db.setTransactionSuccessful();
	}
	public void endTransaction(){
		db.endTransaction();
	}
	
    public void close() 
    {
    	dbAdapter.close();
    }
    
    public long insertAccount(String name, String amount, String currency) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(SQLQuery.KEY_NAME_ACCOUNT, name);
        initialValues.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
        initialValues.put(SQLQuery.KEY_CURRENCY_ACCOUNT, currency);
        return db.insert(SQLQuery.DB_ACCOUNT_TABLE, null, initialValues);
    }

    //---deletes a particular account---
    public boolean deleteAccount(String nameAccount) 
    {
        return db.delete(SQLQuery.DB_ACCOUNT_TABLE,CommonFunct.join(SQLQuery.KEY_NAME_ACCOUNT,
        										" = '",nameAccount,"'"), null) > 0;
    }

    //---retrieves all the accounts---
    public Cursor getAllAccount() 
    {
        return db.query(SQLQuery.DB_ACCOUNT_TABLE, new String[] {
        		SQLQuery.KEY_NAME_ACCOUNT,
        		SQLQuery.KEY_AMOUNT_ACCOUNT}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }
    //---retrieves all the accounts---
    public Cursor getAllAccountByCurrency(String currency) 
    {
        return db.query(SQLQuery.DB_ACCOUNT_TABLE, new String[] {
        		SQLQuery.KEY_NAME_ACCOUNT,
        		SQLQuery.KEY_AMOUNT_ACCOUNT}, 
        		SQLQuery.KEY_CURRENCY_ACCOUNT + " = '" + currency + "'", 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular amount by account name---
    public String getAmountCurrency(String nameAccount) throws SQLException 
    {
    	String strAmount = "0" ;   
        Cursor mCursor =
        	db.query(true, SQLQuery.DB_ACCOUNT_TABLE, new String[] {
                		SQLQuery.KEY_AMOUNT_ACCOUNT,
                		SQLQuery.KEY_CURRENCY_ACCOUNT}, 
                		SQLQuery.KEY_NAME_ACCOUNT + " = '" + nameAccount + "'", 
                		null,
                		null, 
                		null, 
                		null, 
                		null);
        if (mCursor != null) {
        	if (mCursor.moveToFirst()) {
        		strAmount = CommonFunct.join(String.valueOf(mCursor.getDouble(0)),
        				Constant.HASH_STRING,
        				mCursor.getString(1));
		      }
		      if (mCursor != null && !mCursor.isClosed()) {
		    	  mCursor.close();
		      }
        }
        return strAmount;
    }

    //---updates a account---
    public boolean updateAccount( String nameAccount, 
    String amount) 
    {
        ContentValues args = new ContentValues();
        args.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
        return db.update(SQLQuery.DB_ACCOUNT_TABLE, args, 
        		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, null) > 0;
    }
    
}
